<?php
namespace app\wechat\controller;

use think\Db;
use think\View;
use Jssdk\JSSDK;

class Orders extends Common
{
    public function index()
    {
        $status = input('param.status', 0);
        $this->assign('status',$status);
        return view();
    }

    public function get_order_server_list()
    {
        if (request()->isAjax()) {
            $member_id = session('member_id');
            if (!$member_id) {
                return json(['code' => 0, 'msg' => '系统繁忙']);
            }
            $param['member_id'] = $member_id;
            $param['status'] = input('param.status', 0);
            $data = call_interface('order_server_list', $param);
            //dump($data);
            $view = new View();
            if ($data['code'] == 200) {
                $view->assign('data', $data['data']['data']);
            } else {
                $view->assign('data', []);
            }
            $tpl = $view->fetch('orders/order_list_tpl');
            $data['tpl'] = $tpl;
            return json($data);
        }
        return view('base/error');
    }


    public function order_info()
    {
        if( is_weixin() ){
            $jssdk = new JSSDK(config('wechat.appid'), config('wechat.appsecret'));
            $signPackage = $jssdk->GetSignPackage();
            $this->assign('signPackage', $signPackage);
        }

        $order_id = intval(input('param.id',0));
        $param = [
            'order_id' => $order_id
        ];
        $order = call_interface('order_server_info', $param);
//        $order = db('order_server')->alias('o')
//            ->field('o.*,si.item_name,rmr.headimgurl r_headimgurl, mmr.headimgurl m_headimgurl, r.repair_name, r.lat r_lat, r.lon r_lon, r.phone r_phone, oa.id as oa_id, car.name as car_name')
//            ->join('mob_server_items si', 'si.id = o.type', 'LEFT')
//            ->join("mob_repair r", "o.repair_id = r.id", 'LEFT')
//            ->join("mob_member_role_info rmr", "r.member_id = rmr.member_id and rmr.type = 1", 'LEFT')
//            ->join("mob_member_role_info mmr", "o.member_id = mmr.member_id and mmr.type = 0", 'LEFT')
//            ->join("mob_order_austin oa", "oa.order_id = o.id AND oa.type = 2", 'LEFT')
//            ->join("mob_my_car car", "car.member_id = o.member_id", 'LEFT')
//            ->where("o.id",$order_id)->order("car.is_default desc")->find();

        if ($order['code'] == 200) {
            $this->assign('order', $order['data']['order']);
            return view();
        } else {
            $this->error('订单数据获取失败');
            return false;
        }
    }

    // 订单产品列表
    public function server_order_goods() {

        $order_id = input('param.order_id');
        $param = [
            'order_id' => $order_id
        ];
        $order = call_interface('server_order_goods', $param);

        if ($order['code'] != 200) {
            $this->error('获取服务单产品列表数据失败');
            return false;
        }
        $this->assign('order', $order['data']['order']);
        $this->assign('order_goods', $order['data']['order_goods']);
        return view();
    }

    // 取消订单
    public function cancel_order() {
        $id = input('param.id');
        $model = input('param.model');
        $member_id = session('member_id');
        try {
            db($model)->where('id', $id)->where('member_id', $member_id)->update(['status' => 9]);
            return json(['code' => 200, 'msg' => 'success']);
        } catch (\Exception $e) {
            return json(['code' => 0, 'msg' => $e->getMessage()]);
        }
    }

    // 订单完成
    public function affirm_finish() {
        $id = input('param.id');
        $model = input('param.model');
        $member_id = session('member_id');

        $param = [
            'member_id' => $member_id,
            'status' => 6,
            'order_id' => $id,
            'model' => $model
        ];
        $data = call_interface('update_order_server_status', $param);

        return json($data);
    }

    // 重新推送订单
    public function push_again() {
        $id = input('param.id');
        $model = input('param.model');
        $member_id = session('member_id');

        $param = [
            'id' => $id,
            'model' => $model,
            'msg' => input('param.msg'),
            'member_id' => $member_id,
        ];
        $data = call_interface('push_again', $param);
        return json($data);
    }
}
